package com.yln.watcher.client;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.data.Stat;

import java.util.List;

public class GetAssignedTaskCallback implements AsyncCallback.Children2Callback {
    @Override
    public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
        ZkClient zkClient = (ZkClient) ctx;
        KeeperException.Code code = KeeperException.Code.get(rc);
        switch (code) {
            case OK:
                children.forEach(taskNode -> {
                    String payLoad = zkClient.getNodeValue(taskNode);
                    zkClient.doJob(payLoad);
                });
                break;
            case NONODE:
                zkClient.createAssignWorkerNodeAndWatch();
                break;
            default:
                ;

        }
    }
}
